#include "Game.h"

Game::Game (void)
{
}

Game::Game (HINSTANCE Instance, LPCWSTR WindowTitle, int Width, int Height, BOOL Fullscreen)
{
	Init (Instance, WindowTitle, Width, Height, Fullscreen);
}

Game::~Game (void)
{
	if (GameWindow)
		delete GameWindow;
	if (Renderer)
		delete Renderer;
	if (Keyboard)
		delete Keyboard;
	if (Mouse)
		delete Mouse;
}

BOOL Game::Init (HINSTANCE Instance, LPCWSTR WindowTitle, int Width, int Height, BOOL Fullscreen)
{
	BOOL Success = TRUE;
	
	GameWindow = new Window (Instance, WindowTitle, Width, Height, Fullscreen);
	
	if (!GameWindow)
		Success = FALSE;
	else
	{
		Renderer = new DXDevice (*GameWindow);

		if (!Renderer)
			Success = FALSE;
		else
		{
			IsRunning = TRUE;
			State = MAIN_MENU;
			if (!(Keyboard = new DXKeyboard (GameWindow->GetWindowInstance(), GameWindow->GetWindowHandle())))
				Success = FALSE;
			if (!(Mouse = new DXMouse (GameWindow->GetWindowInstance(), GameWindow->GetWindowHandle())))
				Success = FALSE;
			if (!(MMenu = new MainMenu (Renderer, GameWindow->GetScreenWidth(), GameWindow->GetScreenHeight())))
				Success = FALSE;
			if (!(Camera = new DXCamera (Renderer->GetDrawDevice())))
				Success = FALSE;
			else
			{
				Camera->SetCameraPerspective(ToRadian(45), (float)(GameWindow->GetScreenWidth() / GameWindow->GetScreenHeight()), 1.0f, 20.0f);
				Camera->SetCameraPosition(0.0f, 5.0f, -5.0f, 0.0f, 0.0f, 0.0f);
			}
			ShowCursor(FALSE);
			GameWindow->Show();
		}
	}

	// Test Code //
	InGameHolder = new DXText(Renderer->GetDrawDevice(), 50, L"Times New Roman", COLOR(255, 255, 255, 255));
	InGameHolder->SetTextLocation((GameWindow->GetScreenWidth() / 2) - 130, (GameWindow->GetScreenHeight() / 2) - 50);
	InGameHolder->SetText("Insert Game Here.");
	FPS = new DXText(Renderer->GetDrawDevice(), 20, L"Arial", COLOR(255, 255, 255, 255));
	FPS->SetTextLocation(25, 25);
	GameTimer.Start();
	FPS->SetText(GameTimer.GetFPS_String());

	return Success;
}

BOOL Game::Update (void)
{
	if (GameWindow->UpdateWindowMsg())
	{
		switch (State)
		{
			case MAIN_MENU:
				{
					LogicMainMenu();
					RenderMainMenu();
				} break;

			case OPTION_MENU:
				{
					LogicOptionMenu();
					RenderOptionMenu();
				} break;

			case IN_GAME:
				{
					LogicInGame();
					RenderInGame();
				} break;

			case EXIT:
				{
					IsRunning = FALSE;
				} break;
		}
	}
	else
		IsRunning  = FALSE;

	FPS->SetText(GameTimer.GetFPS_String());

	return IsRunning;
}

// In Game //

void Game::LogicInGame (void)
{
	Keyboard->Update();
	if (Keyboard->IsKeyDown(DIK_ESCAPE))
		IsRunning = FALSE;
}

void Game::RenderInGame (void)
{
	Renderer->BeginRendering();
	InGameHolder->Draw();
	FPS->Draw();
	Renderer->EndRendering();
	GameTimer.Update();
}

// Main Menu //

void Game::LogicMainMenu (void)
{
	Keyboard->Update();
	State = MMenu->Update(*Keyboard);
}

void Game::RenderMainMenu (void)
{
	Renderer->BeginRendering();
	Renderer->BeginSprite();
	MMenu->Draw();
	Renderer->EndSprite();
	FPS->Draw();
	Renderer->EndRendering();
	GameTimer.Update();
}

// Option Menu //

void Game::LogicOptionMenu (void)
{
}

void Game::RenderOptionMenu (void)
{
	Renderer->BeginRendering();
	FPS->Draw();
	Renderer->EndRendering();
	GameTimer.Update();
}